package ca.scotthyndman.as2haxe.dom;

import java.util.ArrayList;
import java.util.List;

import ca.scotthyndman.as2haxe.dom.binding.IBinding;
import ca.scotthyndman.as2haxe.dom.visitor.IVisitor;

/**
 * Represents field access
 */
public class ASFieldAccess extends ASExpression {

	/**
	 * The "expression" structural property of this node type.
	 * 
	 * @since 3.0
	 */
	public static final ASChildPropertyDescriptor EXPRESSION_PROPERTY = new ASChildPropertyDescriptor(
			ASFieldAccess.class, "expression", ASExpression.class, true);

	/**
	 * The "name" structural property of this node type.
	 * 
	 * @since 3.0
	 */
	public static final ASChildPropertyDescriptor NAME_PROPERTY = new ASChildPropertyDescriptor(
			ASFieldAccess.class, "name", ASSimpleName.class, true);

	/**
	 * A list of property descriptors (element type:
	 * {@link StructuralPropertyDescriptor}), or null if uninitialized.
	 */
	private static final List<ASPropertyDescriptor> PROPERTY_DESCRIPTORS;

	static {
		List<Object> properyList = new ArrayList<Object>(3);
		createPropertyList(ASFieldAccess.class, properyList);
		addProperty(EXPRESSION_PROPERTY, properyList);
		addProperty(NAME_PROPERTY, properyList);
		PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
	}

	/**
	 * Returns a list of structural property descriptors for this node type.
	 * Clients must not modify the result.
	 * 
	 * @return a list of property descriptors
	 */
	public static List<ASPropertyDescriptor> propertyDescriptors() {
		return PROPERTY_DESCRIPTORS;
	}

	//
	// ======== MEMBERS
	//

	private ASExpression expression;

	private ASSimpleName fieldName;

	public ASFieldAccess(AST ast) {
		super(ast);
	}

	/**
	 * @param expression
	 *            the expression to set
	 */
	public void setExpression(ASExpression expression) {
		if (expression == null) {
			throw new IllegalArgumentException();
		}
		ASTNode oldChild = this.expression;
		preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
		this.expression = expression;
		postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
	}

	/**
	 * @return the expression
	 */
	public ASExpression getExpression() {
		return expression;
	}

	/**
	 * @param fieldName
	 *            the name to set
	 */
	public void setName(ASSimpleName fieldName) {
		if (fieldName == null) {
			throw new IllegalArgumentException();
		}
		ASTNode oldChild = this.fieldName;
		preReplaceChild(oldChild, fieldName, NAME_PROPERTY);
		this.fieldName = fieldName;
		postReplaceChild(oldChild, fieldName, NAME_PROPERTY);
	}

	/**
	 * @return the name
	 */
	public ASSimpleName getName() {
		return fieldName;
	}

	//
	// ======== INTERNAL GETTER / SETTER
	//

	/*
	 * (omit javadoc for this method) Method declared on ASTNode.
	 */
	final ASTNode internalGetSetChildProperty(
			ASChildPropertyDescriptor property, boolean get, ASTNode child) {
		if (property == EXPRESSION_PROPERTY) {
			if (get) {
				return getExpression();
			} else {
				setExpression((ASExpression) child);
				return null;
			}
		}
		if (property == NAME_PROPERTY) {
			if (get) {
				return getName();
			} else {
				setName((ASSimpleName) child);
				return null;
			}
		}
		// allow default implementation to flag the error
		return super.internalGetSetChildProperty(property, get, child);
	}

	// ======== BINDINGS

	/**
	 * Returns the binding linked to this field access.
	 */
	public IBinding resolveBinding() {
		return getAST().getBindingManager().resolveFieldAccessBinding(this);
	}

	// ===== TOSTRING AND VISITOR

	@Override
	public String toString() {
		if (getExpression() != null) {
			return getExpression().toString() + "." + getName().toString();
		}
		return getName().toString();
	}

	@Override
	public void accept(IVisitor as2Visitor, boolean recursive) {
		recursive = as2Visitor.visit(this) && recursive;

		if (recursive) {
			if (getExpression() != null) {
				getExpression().accept(as2Visitor, recursive);
			}

			getName().accept(as2Visitor, recursive);
		}
	}

}
